using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AIC_KQKT
{
    public class clsDataBind
    {
        static public void CreateCombo(SqlDataReader Reader, DropDownList drop, int iText, int iValue)
        {
            drop.DataTextField = Reader.GetName(iText);
            drop.DataValueField = Reader.GetName(iValue);
            drop.DataSource = Reader;
            drop.DataBind();
            Reader.Close();
        }

        static public void CreateCombo(string sTitle, SqlDataReader Reader, DropDownList drop, int iText, int iValue)
        {
            System.Web.UI.WebControls.ListItem temItem;
            string sTextColum = Reader.GetName(iText);
            string sValueColum = Reader.GetName(iValue);
            drop.Items.Clear();
            // them tieu de
            temItem = new ListItem(sTitle, "0");
            drop.Items.Add(temItem);

            while (Reader.Read())
            {
                temItem = new ListItem();
                temItem.Text = Reader[sTextColum].ToString();
                temItem.Value = Reader[sValueColum].ToString();
                drop.Items.Add(temItem);

            }
            Reader.Close();
        }

        static public void CreateRadioList(SqlDataReader Reader, RadioButtonList rdb, int iText, int iValue)
        {
            rdb.DataTextField = Reader.GetName(iText);
            rdb.DataValueField = Reader.GetName(iValue);
            rdb.DataSource = Reader;
            rdb.DataBind();
            Reader.Close();
        }
        static public void CreateCheckBoxList(SqlDataReader Reader, CheckBoxList chklist, int iText, int iValue)
        {
            chklist.DataTextField = Reader.GetName(iText);
            chklist.DataValueField = Reader.GetName(iValue);
            chklist.DataSource = Reader;
            chklist.DataBind();
            Reader.Close();
        }
        static public void CreateCheckBoxListMenu(SqlDataReader Reader, CheckBoxList chklist, int iText, int iValue)
        {
            System.Web.UI.WebControls.ListItem temItem;
            string sTextColum = Reader.GetName(iText);
            string sValueColum = Reader.GetName(iValue);
            chklist.Items.Clear();

            string sLevel = "0";
            int iLevel = 0; ;
            while (Reader.Read())
            {
                sLevel = Reader["ContentItemLevel"].ToString();
                iLevel = Convert.ToInt16(sLevel);

                sLevel = "";
                for (int i = 0; i < iLevel; i++)
                {
                    sLevel += "____";
                }

                temItem = new ListItem();
                temItem.Text = sLevel + Reader[sTextColum].ToString();
                temItem.Value = Reader[sValueColum].ToString();
                chklist.Items.Add(temItem);

            }
            Reader.Close();
        }



        static public void CreateGrid(SqlDataReader Reader, GridView grid)
        {
            grid.DataSource = Reader;
            grid.DataBind();
            Reader.Close();
        }

        static public DataTable GetGenreTable(SqlDataReader dreader)
        {
            DataTable dtGenre = new DataTable();
            DataRow dr;
            for (int i = 0; i < dreader.FieldCount; i++)
            {
                DataColumn dc = new DataColumn(dreader.GetName(i));
                dtGenre.Columns.Add(dc);
            }
            while (dreader.Read())
            {
                dr = dtGenre.NewRow();
                for (int i = 0; i < dreader.FieldCount; i++)
                {
                    dr[i] = dreader[i];
                }
                dtGenre.Rows.Add(dr);
            }
            dreader.Close();
            return dtGenre;
        }


        static public void CreateGridTable(SqlDataReader Reader, GridView grid)
        {
            DataTable myDataTable;
            myDataTable = GetGenreTable(Reader);
            grid.DataSource = myDataTable;
            grid.DataBind();
            myDataTable.Dispose();
            Reader.Close();
        }

        static public void CreateGridTable(DataGrid datagrid, DataTable datatable)
        {
            datagrid.DataSource = datatable;
            datagrid.DataBind();
        }

        static public void CreateGridTable(GridView datagrid, DataTable datatable)
        {
            datagrid.DataSource = datatable;
            datagrid.DataBind();
        }


        static public void CreateGridDataView(DataView dataView, DataGrid grid)
        {
            grid.DataSource = dataView;
            grid.DataBind();
        }

        static public void CreateGridDataView(DataView dataView, GridView grid)
        {
            grid.DataSource = dataView;
            grid.DataBind();
        }

        static public void CreateDataList(SqlDataReader Reader, DataList list)
        {
            list.DataSource = Reader;
            list.DataBind();
            Reader.Close();
        }
        static public int FindCombo(DropDownList drop, String key)
        {
            ListItem crItem = null;
            crItem = drop.Items.FindByValue(key);
            return drop.Items.IndexOf(crItem);
        }
        static public int FindComboByVal(DropDownList drop, String valkey)
        {
            ListItem crItem = null;
            crItem = drop.Items.FindByValue(valkey);
            return drop.Items.IndexOf(crItem);
        }
        static public int FindComboByText(DropDownList drop, String textkey)
        {
            ListItem crItem = null;
            crItem = drop.Items.FindByText(textkey);
            return drop.Items.IndexOf(crItem);
        }
        static public int FindChklist(CheckBoxList chklist, String key)
        {
            ListItem chkItem = null;
            chkItem = chklist.Items.FindByValue(key);
            return chklist.Items.IndexOf(chkItem);
        }
    }
}
